package com.taxi.driver.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.qianxx.view.wheel.adapter.ArrayWheelAdapter;
import com.qianxx.view.wheel.hh.OnWheelChangedListener;
import com.qianxx.view.wheel.hh.WheelView;
import com.taxi.driver.R;
import com.taxi.driver.util.TimeUtils;

/**
 * 对话框工具类
 */
public class DialogCreator {
    /**
     * 选择座位数对话框
     * @param context
     * @param listener
     * @return
     */
    public static Dialog selectSeatNumberDialog(Context context, final OnNumberListener listener) {
        Dialog dialog = new Dialog(context, R.style.select_image_dialog);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_select_seat_number, null);
        dialog.setContentView(view);

        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);

        TextView cancel = (TextView) view.findViewById(R.id.cancel);
        TextView confirm = (TextView) view.findViewById(R.id.confirm);
        final WheelView wv = (WheelView) view.findViewById(R.id.wv);
        //设置WheelView在屏幕中最大显示元素个数
        wv.setVisibleItems(3);
        //设置WheelView 元素是否循环滚动
        wv.setCyclic(false);
        //设置WheelView 的元素适配器
        Integer[] data = {1,2,3,4,5,6};
        wv.setViewAdapter(new ArrayWheelAdapter<Integer>(context,data));
        wv.setCurrentItem(3);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onCancel();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onComfig(data[wv.getCurrentItem()]);
                dialog.dismiss();
            }
        });

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);
        return dialog;
    }

    private static String[] time;
    private static String[] temp;
    /**
     * 时间选择对话框
     * @param context
     * @param listener
     * @return
     */
    public static Dialog selectTimeDialog(Context context, final OnTimeListener listener) {
        Dialog dialog = new Dialog(context, R.style.select_image_dialog);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_select_time, null);
        dialog.setContentView(view);

        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);

        TextView cancel = (TextView) view.findViewById(R.id.cancel);
        TextView confirm = (TextView) view.findViewById(R.id.confirm);

        final WheelView wv_date = (WheelView) view.findViewById(R.id.wv_date);
        wv_date.setVisibleItems(3);
        wv_date.setCyclic(false);
        String[] date = new String[15];
        if(TimeUtils.getCurTime().compareTo("23:00")>0 && TimeUtils.getCurTime().compareTo("24:00")<0){
            temp = TimeUtils.getDates2();
        }else{
            temp = TimeUtils.getDates();
        }
        for(int i=0;i<temp.length;i++){
            if(temp[i].equals(TimeUtils.getTodayWithYear())){
                date[i] = "今天";
            }else if(temp[i].equals(TimeUtils.getTomorrowWithYear())){
                date[i] = "明天";
            }else if(temp[i].equals(TimeUtils.getBermorgenWithYear())){
                date[i] = "后天";
            }else{
                date[i] = temp[i].substring(5,10);
            }
        }
        wv_date.setViewAdapter(new ArrayWheelAdapter<String>(context,date));
        wv_date.setCurrentItem(0);

        final WheelView wv_time = (WheelView) view.findViewById(R.id.wv_time);
        wv_time.setVisibleItems(3);
        wv_time.setCyclic(false);
        wv_time.setCurrentItem(0);

        if(TimeUtils.getCurTime().compareTo("23:30")>0 && TimeUtils.getCurTime().compareTo("24:00")<0){
            time = TimeUtils.getAllTimes2();
        }else{
            time = TimeUtils.getTimes();
        }
        wv_time.setViewAdapter(new ArrayWheelAdapter<String>(context,time));
        wv_date.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                if(wheel.getCurrentItem() == 0){
                    if(TimeUtils.getCurTime().compareTo("23:30")>0 && TimeUtils.getCurTime().compareTo("24:00")<0){
                        time = TimeUtils.getAllTimes2();
                    }else {
                        time = TimeUtils.getTimes();
                    }
                    wv_time.setViewAdapter(new ArrayWheelAdapter<String>(context,time));
                }else{
                    time = TimeUtils.getAllTimes();
                    wv_time.setViewAdapter(new ArrayWheelAdapter<String>(context,time));
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onCancel();
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onComfig(temp[wv_date.getCurrentItem()].substring(0,4),date[wv_date.getCurrentItem()],time[wv_time.getCurrentItem()]);
                }
                dialog.dismiss();
            }
        });
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);
        return dialog;
    }

    /**
     * 常见的确认/取消，对话框
     *
     * @param context
     * @param titleStr
     * @param listener
     * @return
     */
    public static Dialog defaultDialog(Context context, String titleStr,boolean showCancel,OnDefaultListener listener) {
        Dialog dialog = new Dialog(context, R.style.select_image_dialog);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_default, null);
        TextView title = (TextView) view.findViewById(R.id.tv_title);
        title.setText(titleStr);
        TextView confirm = (TextView) view.findViewById(R.id.tv_confirm);
        TextView cancel = (TextView) view.findViewById(R.id.tv_cancel);

        if(showCancel){
            cancel.setVisibility(View.VISIBLE);
        }else{
            cancel.setVisibility(View.GONE);
            confirm.setBackgroundResource(R.drawable.sel_dialog_btn);
        }

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.onCancel();
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.onComfig();
            }
        });

        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);
        return dialog;
    }

}
